package de.bbl.android.tools;

import java.io.File;
import java.io.IOException;
import java.util.ArrayList;

import org.jsoup.Jsoup;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

import de.bbl.android.classes.Video;

public class HtmlParseTools {

	public static Video[] ParseVideos(String filename) {
		Elements nodeBlogStats = null;
		ArrayList<Video> retValList = new ArrayList<Video>();
		try {
			Document doc = Jsoup.parse(new File(filename), "UTF-8");
			nodeBlogStats = doc.select("div#featured div ol li a");
			for (int i = 0; i < nodeBlogStats.size(); i++) {
				Element element = nodeBlogStats.get(i);
				// test = element.text();	
				// test = element.attr("href");
				retValList.add(new Video(element.attr("href"),element.text()));
			}
			
			nodeBlogStats = doc.select("div#related div div ol li a");

			for (int i = 0; i < nodeBlogStats.size(); i++) {
				Element element = nodeBlogStats.get(i);
				// test = element.text();	
				// test = element.attr("href");
				retValList.add(new Video(element.attr("href"),element.text()));
			}

		} catch (IOException e1) {
			e1.printStackTrace();
		}

		return retValList.toArray(new Video[1]);
	}

	public static String ParseSingleVideoLink(String url) {
		Elements nodeBlogStats = null;
		String retval = "";
		try {
			Document doc = Jsoup.connect(url).get();
			nodeBlogStats = doc.select("div#player a");

			for (int i = 0; i < nodeBlogStats.size(); i++) {
				Element element = nodeBlogStats.get(i);
				retval = element.attr("href");
			}

		} catch (IOException e1) {
			e1.printStackTrace();
		}
		return retval;
		
	}

}
